package ldh.im.fxgui.component.cell;

import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.image.ImageView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import ldh.im.fxbase.data.pojo.ImGroup;
import ldh.im.fxbase.data.pojo.ImGroupUser;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.util.ImCacheFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static ldh.im.fxgui.util.UserHeadImageUtil.getHeadImage;

public class GroupIconView extends GridPane {

    private ImGroup imGroup;

    public GroupIconView(ImGroup imGroup) {
        this.imGroup = imGroup;
        initGridView();
        initUi();
    }

    private void initUi() {
        List<ImageView> imageViews = new ArrayList<>();
        int i= 0;
        Set<ImGroupUser> groupUserList = imGroup.getGroupUserSet();
        for(ImGroupUser groupUser : groupUserList) {
            ImUser imUser = ImCacheFactory.getInstance().getUser(groupUser.getUserId());
            if (imUser.getHeadImg() == null) continue;
            i++;
            ImageView imageView = buildImageView(groupUser.getUserId());
            imageViews.add(imageView);
            if(i >= 9) {
                break;
            }
        }

        if (imageViews.size() < 9) {
            i = imageViews.size();
            for(ImGroupUser groupUser : groupUserList) {
                ImUser imUser = ImCacheFactory.getInstance().getUser(groupUser.getUserId());
                if (imUser.getHeadImg() != null) continue;
                i++;
                ImageView imageView = buildImageView(groupUser.getUserId());
                imageViews.add(imageView);
                if(i >= 9) {
                    break;
                }
            }
        }

        initGridChildren(imageViews);
    }

    private void initGridChildren(List<ImageView> imageViews) {
        this.getChildren().addAll(imageViews);
        if (imageViews.size() == 2) {
            imageViews.forEach(imageView -> fitImageViewSize(imageView, 36/2));
            GridPane.setConstraints(imageViews.get(0), 0, 3, 6, 6);
            GridPane.setConstraints(imageViews.get(1), 9, 3, 6, 6);
        } else if (imageViews.size() == 3) {
            imageViews.forEach(imageView -> fitImageViewSize(imageView, 36/2));
            GridPane.setConstraints(imageViews.get(0), 3, 0, 6, 6);
            GridPane.setConstraints(imageViews.get(1), 0, 6, 6, 6);
            GridPane.setConstraints(imageViews.get(2), 6, 6, 6, 6);
        } else if (imageViews.size() == 4) {
            imageViews.forEach(imageView -> fitImageViewSize(imageView, 36/2));
            GridPane.setConstraints(imageViews.get(0), 0, 0, 6, 6);
            GridPane.setConstraints(imageViews.get(1), 6, 0, 6, 6);
            GridPane.setConstraints(imageViews.get(2), 0, 6, 6, 6);
            GridPane.setConstraints(imageViews.get(3), 6, 6, 6, 6);
        } else if (imageViews.size() == 5) {
            imageViews.forEach(imageView -> fitImageViewSize(imageView, 36/3));
            GridPane.setConstraints(imageViews.get(0), 2, 2, 4, 4);
            GridPane.setConstraints(imageViews.get(1), 7, 2, 4, 4);
            GridPane.setConstraints(imageViews.get(2), 0, 6, 4, 4);
            GridPane.setConstraints(imageViews.get(3), 4, 6, 4, 4);
            GridPane.setConstraints(imageViews.get(4), 8, 6, 4, 4);
        } else if (imageViews.size() == 6) {
            imageViews.forEach(imageView -> fitImageViewSize(imageView, 36/3));
            GridPane.setConstraints(imageViews.get(0), 0, 2, 4, 4);
            GridPane.setConstraints(imageViews.get(1), 4, 2, 4, 4);
            GridPane.setConstraints(imageViews.get(2), 8, 2, 4, 4);
            GridPane.setConstraints(imageViews.get(3), 0, 6, 4, 4);
            GridPane.setConstraints(imageViews.get(4), 4, 6, 4, 4);
            GridPane.setConstraints(imageViews.get(5), 8, 6, 4, 4);
        } else if (imageViews.size() == 7) {
            imageViews.forEach(imageView -> fitImageViewSize(imageView, 36/3));
            GridPane.setConstraints(imageViews.get(0), 4, 0, 4, 4);
            GridPane.setConstraints(imageViews.get(1), 0, 4, 4, 4);
            GridPane.setConstraints(imageViews.get(2), 4, 4, 4, 4);
            GridPane.setConstraints(imageViews.get(3), 8, 4, 4, 4);
            GridPane.setConstraints(imageViews.get(4), 0, 8, 4, 4);
            GridPane.setConstraints(imageViews.get(5), 4, 8, 4, 4);
            GridPane.setConstraints(imageViews.get(6), 8, 8, 4, 4);
        } else if (imageViews.size() == 8) {
            imageViews.forEach(imageView -> fitImageViewSize(imageView, 36/3));
            GridPane.setConstraints(imageViews.get(0), 2, 0, 4, 4);
            GridPane.setConstraints(imageViews.get(1), 6, 0, 4, 4);
            GridPane.setConstraints(imageViews.get(2), 0, 4, 4, 4);
            GridPane.setConstraints(imageViews.get(3), 4, 4, 4, 4);
            GridPane.setConstraints(imageViews.get(4), 8, 4, 4, 4);
            GridPane.setConstraints(imageViews.get(5), 0, 8, 4, 4);
            GridPane.setConstraints(imageViews.get(6), 4, 8, 4, 4);
            GridPane.setConstraints(imageViews.get(7), 8, 8, 4, 4);
        } else if (imageViews.size() == 9) {
            imageViews.forEach(imageView -> fitImageViewSize(imageView, 36/3));
            GridPane.setConstraints(imageViews.get(0), 0, 0, 4, 4);
            GridPane.setConstraints(imageViews.get(1), 4, 0, 4, 4);
            GridPane.setConstraints(imageViews.get(2), 8, 0, 4, 4);
            GridPane.setConstraints(imageViews.get(3), 0, 4, 4, 4);
            GridPane.setConstraints(imageViews.get(4), 4, 4, 4, 4);
            GridPane.setConstraints(imageViews.get(5), 8, 4, 4, 4);
            GridPane.setConstraints(imageViews.get(6), 0, 8, 4, 4);
            GridPane.setConstraints(imageViews.get(7), 4, 8, 4, 4);
            GridPane.setConstraints(imageViews.get(8), 8, 8, 4, 4);
        }
    }

    private void initGridView() {
        RowConstraints rc1 = new RowConstraints(3, 3, 3);
        rc1.setValignment(VPos.CENTER);
        RowConstraints rc2 = new RowConstraints(3, 3, 3);
        rc2.setValignment(VPos.CENTER);
        RowConstraints rc3 = new RowConstraints(3, 3, 3);
        rc3.setValignment(VPos.CENTER);
        RowConstraints rc4 = new RowConstraints(3, 3, 3);
        rc4.setValignment(VPos.CENTER);
        RowConstraints rc5 = new RowConstraints(3, 3, 3);
        rc5.setValignment(VPos.CENTER);
        RowConstraints rc6 = new RowConstraints(3, 3, 3);
        rc6.setValignment(VPos.CENTER);
        RowConstraints rc7 = new RowConstraints(3, 3, 3);
        rc7.setValignment(VPos.CENTER);
        RowConstraints rc8 = new RowConstraints(3, 3, 3);
        rc8.setValignment(VPos.CENTER);
        RowConstraints rc9 = new RowConstraints(3, 3, 3);
        rc9.setValignment(VPos.CENTER);
        RowConstraints rc10 = new RowConstraints(3, 3, 3);
        rc10.setValignment(VPos.CENTER);
        RowConstraints rc11 = new RowConstraints(3, 3, 3);
        rc11.setValignment(VPos.CENTER);
        RowConstraints rc12 = new RowConstraints(3, 3, 3);
        rc12.setValignment(VPos.CENTER);
        this.getRowConstraints().addAll(rc1, rc2, rc3, rc4, rc5, rc6, rc7, rc8, rc9, rc10, rc11, rc12);

        ColumnConstraints c1 = new ColumnConstraints(3, 3, 3);
        c1.setHalignment(HPos.CENTER);
        ColumnConstraints c2 = new ColumnConstraints(3, 3, 3);
        c2.setHalignment(HPos.CENTER);
        ColumnConstraints c3 = new ColumnConstraints(3, 3, 3);
        c3.setHalignment(HPos.CENTER);
        ColumnConstraints c4 = new ColumnConstraints(3, 3, 3);
        c4.setHalignment(HPos.CENTER);
        ColumnConstraints c5 = new ColumnConstraints(3, 3, 3);
        c5.setHalignment(HPos.CENTER);
        ColumnConstraints c6 = new ColumnConstraints(3, 3, 3);
        c6.setHalignment(HPos.CENTER);
        ColumnConstraints c7 = new ColumnConstraints(3, 3, 3);
        c7.setHalignment(HPos.CENTER);
        ColumnConstraints c8 = new ColumnConstraints(3, 3, 3);
        c8.setHalignment(HPos.CENTER);
        ColumnConstraints c9 = new ColumnConstraints(3, 3, 3);
        c9.setHalignment(HPos.CENTER);
        ColumnConstraints c10 = new ColumnConstraints(3, 3, 3);
        c10.setHalignment(HPos.CENTER);
        ColumnConstraints c11 = new ColumnConstraints(3, 3, 3);
        c11.setHalignment(HPos.CENTER);
        ColumnConstraints c12 = new ColumnConstraints(3, 3, 3);
        c12.setHalignment(HPos.CENTER);
        this.getColumnConstraints().addAll(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12);
    }

    private ImageView buildImageView(String userId) {
        ImageView imageView = new ImageView();
//        imageView.setFitWidth(10);
//        imageView.setFitHeight(10);
        ImUser imUser = ImCacheFactory.getInstance().getUser(userId);
        imageView.setImage(getHeadImage(imUser.getHeadImg()));
        return imageView;
    }

    private void fitImageViewSize(ImageView imageView, double size) {
        imageView.setFitHeight(size);
        imageView.setFitWidth(size);
    }
}
